package com.lxp.project.function.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.lxp.common.constant.HttpStatus;
import com.lxp.common.utils.StringUtils;
import com.lxp.common.utils.poi.ExcelUtil;
import com.lxp.framework.web.domain.AjaxResult;
import com.lxp.framework.web.page.TableDataInfo;
import org.springframework.stereotype.Service;
import com.lxp.project.function.mapper.FnProductMapper;
import com.lxp.project.function.domain.FnProduct;
import com.lxp.project.function.service.IFnProductService;

import javax.annotation.Resource;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 产品管理Service业务层处理
 *
 * @author 乐乐
 * @date 2022-05-04
 */
@Service
public class FnProductServiceImpl extends ServiceImpl<FnProductMapper, FnProduct> implements IFnProductService {
    @Resource
    private FnProductMapper fnProductMapper;

    @Override
    public List<FnProduct> getAllList() {
        return fnProductMapper.selectList(null);
    }

    @Override
    public TableDataInfo getPageList(FnProduct fnProduct, TableDataInfo tableSupport) {
        LambdaQueryWrapper<FnProduct> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(fnProduct.getName())) {
            queryWrapper.like(FnProduct::getName, fnProduct.getName());
        }
        Integer count = fnProductMapper.selectCount(queryWrapper);
        IPage<FnProduct> iPage = fnProductMapper.selectPage(new Page<>(tableSupport.getPageNum(), tableSupport.getPageSize()), queryWrapper);
        tableSupport.setCode(HttpStatus.SUCCESS);
        tableSupport.setMsg("查询成功");
        tableSupport.setRows(iPage.getRecords());
        tableSupport.setTotal(count);
        return tableSupport;
    }

    @Override
    public AjaxResult saveData(FnProduct fnProduct) {
        return AjaxResult.msg(fnProductMapper.insert(fnProduct));
    }

    @Override
    public AjaxResult updateDataById(FnProduct fnProduct) {
        return AjaxResult.msg(fnProductMapper.updateById(fnProduct));
    }

    @Override
    public AjaxResult removeByIds(List<Long> ids) {
        return AjaxResult.msg(fnProductMapper.deleteBatchIds(ids));
    }

    @Override
    public AjaxResult exports(FnProduct fnProduct) {
        LambdaQueryWrapper<FnProduct> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(fnProduct.getName())) {
            queryWrapper.like(FnProduct::getName, fnProduct.getName());
        }
        List<FnProduct> list = fnProductMapper.selectList(queryWrapper);
        ExcelUtil<FnProduct> util = new ExcelUtil<>(FnProduct.class);
        return util.exportExcel(list, "fnProduct");
    }

    @Override
    public FnProduct getById(Long id) {
        return fnProductMapper.selectById(id);
    }
}
